package ControlLayer;
import ModelLayer.*;


/**
 * Write a description of class LoanListController here.
 * 
 * @author (Group1:Georgi,Roland and Mindaugas) 
 * @version (a version number or a date)
 */
public class LoanListController
{
    private LoanList list;
    private DVDCollection collection;
    private AddressBook book;
    
    public LoanListController()
    {
        book=AddressBook.getAddressBook();
        collection=DVDCollection.getDVDCollection();
        list = LoanList.getLoanList();
       
    }

    public boolean loanExist(long loanID)
    {
        return list.loanExist(loanID);
    }
    
    /**
     * Calls the personExist method in the model layers Addressbook class.
     */
    public boolean personExist(long personID)
    {
     return book.personExist(personID);
    }
    
    /**
     * Calls the DVDExist method in the model layers DVDCollection class.
     */
    public boolean dvdExist(long dvdID)
    {
     return collection.dvdExist(dvdID);
    }
    
    /**
     * Calls the copyExist method in the model layers DVDCollection class.
     */
    public boolean copyExist(long dvdID,long copySerial)
    {
     return collection.copyExist(dvdID,copySerial);
    }
    
    /**
     * Calls the createLoand method from the model layers LoanListController and makes sure that two loans with the same id can not be created.
     */
    public void createLoan(long loanID,int borrowPeriod,long personID,long dvdSerial,long copyID)
    {
        DVD dvd=collection.searchDVD(dvdSerial);
        Copy copy=dvd.searchCopy(copyID);
        Person person=book.searchPerson(personID);
        
        if(copy!=null && person!=null)
          {
              Loan loan=list.createLoan(loanID,borrowPeriod,person,copy);
              if(loan!=null)
              {
                person.addLoan(loan);
                copy.addLoan(loan);
                }
                
          }
        else
          throw new RuntimeException();
    }
    
    /**
     * calls mutator methods
     */
   private void setBorrowDate(Loan loan,String date)
   {
     loan.setBorrowDate(date);  
   }
   
   public void setBorrowDate(long loanID,String date)
   {
    Loan loan=list.searchLoan(loanID);
    setBorrowDate(loan,date);
    }
    
    /**
     * Checks if the DVD is returned.
     */
   public void DVDisReturned(long personID,long dvdSerial,long copyID)
    {
        DVD dvd=collection.searchDVD(dvdSerial);
        Copy copy=dvd.searchCopy(copyID);
        Person person=book.searchPerson(personID);
        
        if(person!=null && copy!=null)
        {
          Loan loan=list.searchLoan(person,copy);
          
            if(loan!=null)
             {
              list.deleteLoan(loan);
              person.deleteLoan(loan);
              copy.deleteLoan(loan);
             
             }
            
        }
    }  
    
    /**
     * Updates the Loans details
     */
   public void updateLoan(long id, String borrowDate, int period, String state)
    {
        updateDVDBorrowDate(id, borrowDate);
        updateDVDPeriod(id, period);
        updateDVDState(id, state);
    }
       
    /**
     * Searches a Loan using its id through the model layers LoanList and calls its Mutator Methods.
     */
   public void updateDVDBorrowDate(long id, String borrowDate)
    {
        list.searchLoan(id).setBorrowDate(borrowDate);
    }
    
   public void updateDVDPeriod(long id, int period)
    {
        list.searchLoan(id).setPeriod(period);
    }
    
   public void updateDVDState(long id, String state)
    {
        list.searchLoan(id).setState(state);
    }
    
     public void updateDVDState(long id, int days)
    {
        list.searchLoan(id).updateStateAccordingDays(days);
    }
    
    /**
     * Sends a command to search after a loan using its id through model layers Loan List and calls its  Accesor methods.
     */
    public String getDVDBorrowDate(long id)
    {
       return list.searchLoan(id).getBorrowDate();
    }
    
    public int getDVDPeriod(long id)
    {
        return list.searchLoan(id).getPeriod();
    }
    
    public String getDVDState(long id)
    {
        return list.searchLoan(id).getState();
    }
    
    public Long getLoanID(long personID,long dvdSerial,long copyID)
    {
        
        DVD dvd=collection.searchDVD(dvdSerial);
        Copy copy=dvd.searchCopy(copyID);
        Person person=book.searchPerson(personID);
        
        if(person!=null && copy!=null)
        {
          Loan loan=list.searchLoan(person,copy);
          
          return loan.getID();
            
        }
        
        else
          throw new RuntimeException();
    }
        
        
}
    
  

